﻿using DAL;
using DAL.Dao;
using DataProj.Areas.BackSys.ServiceTools;
using Model;
using MQTools;
using Newtonsoft.Json;
using RabbitMQ.Client;
using System;
using System.Collections.Generic;
using System.Diagnostics;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Web;
using System.Web.Mvc;

namespace DataProj.Areas.BackSys.Controllers
{
    public class BaseController : Controller, IDisposable
    {


        protected static MQSender MqSender { get; private set; }
        protected static MQReceiver MqReceiver { get; private set; }

        protected const string ReceiverRoute = "#.{0}.{1}";
        protected const string SenderRoute = "{0}.{1}.{2}";
        public static string LoginType { get; set; }
        public static int UserID { get; set; }


       public static SingalRSender SingalRSender { get; private set; }
        // GET: Base
        //public  ActionResult Index()
        //{
        //    //if (Session[Common.AdminLoginSessionKey] == null)
        //    //{

        //    //}
        //    return View();
        //}
        public BaseController()
        {
            MySession.WriteSession("LoadOver", string.Empty);
            if (SingalRSender==null) SingalRSender = new SingalRSender();
        }

        
        protected async void ReactCustomer(QueueMessage queueMessage)
        {
            string tips = string.Empty;
            string bagID = string.Empty;
            int complaintID = 0;
            
            using (MessageDaoimpl _impl = new MessageDaoimpl())
            {
                string topic = string.Empty;
                switch (queueMessage.Action)
                {
                    case QueueMessage.ActionType.USER_REGISTER:
                        tips = string.Format("新用户注册{0}", queueMessage.User.ID);
                        topic = "用户";
                        break;
                    case QueueMessage.ActionType.BAG_REGISTRER:
                        tips = string.Format("新快递登记{0}", queueMessage.Bag.BagID);
                        topic = "快递";
                        bagID = queueMessage.Bag.BagID;
                        break;
                    case QueueMessage.ActionType.BAG_UPDATE:
                        tips = string.Format("快递更新{0}", queueMessage.Bag.BagID);
                        topic = "快递";
                        bagID = queueMessage.Bag.BagID;
                        break;
                    case QueueMessage.ActionType.BAG_DELETE:
                        tips = string.Format("快递删除{0}", queueMessage.Bag.BagID);
                        topic = "快递";
                        bagID = queueMessage.Bag.BagID;
                        break;
                    case QueueMessage.ActionType.COMPLAINT_REPLY:
                        tips = string.Format("投诉有了回复{0}", queueMessage.Complaint.ID);
                        topic = "投诉";
                        complaintID = queueMessage.Complaint.ID;
                        break;
                    case QueueMessage.ActionType.COMPLAINT_SUBMIT:
                        tips = string.Format("有新的投诉上传{0}", queueMessage.Complaint.ID);
                        topic = "投诉";
                        complaintID = queueMessage.Complaint.ID;
                        break;
                    case QueueMessage.ActionType.COMPLAINT_DELETE:
                        tips = string.Format("有一则投诉删除{0}", queueMessage.Complaint.ID);
                        topic = "投诉";
                        complaintID = queueMessage.Complaint.ID;
                        break;
                    case QueueMessage.ActionType.COMPLAINT_UPDATE:
                        tips = string.Format("有一则投诉更新信息{0}", queueMessage.Complaint.ID);
                        topic = "投诉";
                        complaintID = queueMessage.Complaint.ID;
                        break;
                    default:
                        break;
                }
                var message = _impl.Add(new Message()
                {
                    Info = tips,
                    UserID = queueMessage.User.ID,
                    Topic = topic,
                    AddTime = DateTime.Now,
                    ComplaintID = complaintID,
                    BagID = bagID
                });
                await SingalRSender.SendSysToOneByUserId(queueMessage.User.ID,JsonConvert.SerializeObject(message));
                Debug.WriteLine(tips);
            }
               
          
    
        }

        ~BaseController()
        {
 
        }
       

        protected void ResetMQLink()
        {
            if (MqReceiver.Connection.IsOpen) MqReceiver.Connection.Abort();
            MqReceiver = null;
            MqSender = null;

        }
        protected  async void  ReactCompany(QueueMessage queueMessage)
        {
            string tips = string.Empty;
            string topic = string.Empty;
            string bagID = string.Empty;
            int complaintID = 0;
            using (MessageDaoimpl _impl = new MessageDaoimpl())
            {
                switch (queueMessage.Action)
                {
                    case QueueMessage.ActionType.USER_REGISTER:
                        tips = string.Format("新用户注册{0}", queueMessage.User.ID);
                        topic = "用户";
                        break;
                    case QueueMessage.ActionType.BAG_REGISTRER:
                        tips = string.Format("新快递注册{0}", queueMessage.Bag.BagID);
                        bagID = queueMessage.Bag.BagID;
                        topic = "快递";
                        break;
                    case QueueMessage.ActionType.BAG_UPDATE:
                        tips = string.Format("快递更新{0}", queueMessage.Bag.BagID);
                        bagID = queueMessage.Bag.BagID;
                        topic = "快递";
                        break;
                    case QueueMessage.ActionType.BAG_DELETE:
                        tips = string.Format("快递删除{0}", queueMessage.Bag.BagID);
                        bagID = queueMessage.Bag.BagID;
                        topic = "快递";
                        break;
                    case QueueMessage.ActionType.COMPLAINT_SUBMIT:
                        tips = string.Format("有新的投诉上传{0}", queueMessage.Complaint.ID);
                        complaintID = queueMessage.Complaint.ID;
                        topic = "投诉";
                        break;
                    case QueueMessage.ActionType.COMPLAINT_DELETE:
                        tips = string.Format("有一则投诉删除{0}", queueMessage.Complaint.ID);
                        complaintID = queueMessage.Complaint.ID;
                        topic = "投诉";
                        break;
                    case QueueMessage.ActionType.COMPLAINT_UPDATE:
                        tips = string.Format("有一则投诉更新信息{0}", queueMessage.Complaint.ID);
                        complaintID = queueMessage.Complaint.ID;
                        topic = "投诉";
                        break;
                    default:
                        break;
                }
                _impl.Add(new DAL.Message
                {
                    Info = tips,
                    UserID = queueMessage.User.ID,
                    Topic = topic,
                    AddTime = DateTime.Now,
                    ComplaintID = complaintID,
                    BagID = bagID
                });
                await SingalRSender.SendSysToGroup("Company",tips.ToString());
                Debug.WriteLine(tips);
            }
         
           
            //Debug.WriteLine(string.Format(tipTemplate, tips));
            //Response.Write(string.Format(tipTemplate, tips));
            //Response.End();
        }

        protected override void OnActionExecuting(ActionExecutingContext filterContext)
        {
            
            base.OnActionExecuting(filterContext);
            var controllerName = filterContext.ActionDescriptor.ControllerDescriptor.ControllerName;
       

            LoginType = MySession.GetSession("loginType");
           
            if (string.IsNullOrEmpty(LoginType)|| MySession.GetSession("detail") == null)
            {
                //重定向至登录页面
                filterContext.Result = RedirectToAction("Login", "Home");
                return;
            }
            UserID = Convert.ToInt16(MySession.GetSession("userID"));
            if (MqSender == null)
            {
                MqSender = new MQSender(Resources.HostName, Resources.VirtualHost, Resources.UserName, Resources.Password, Resources.Exchange, ExchangeType.Topic);
            }
            if (MqReceiver == null)
            {
                MqReceiver = new MQReceiver(Resources.HostName, Resources.VirtualHost, Resources.UserName, Resources.Password, Resources.Exchange, ExchangeType.Topic);
                MqReceiver.Bind(Resources.CustomerBagQueue, Resources.CustomerBagRoute, ReactCustomer);
                MqReceiver.Bind(Resources.CustomerComplaintQueue, Resources.CustomerComplaintRoute, ReactCustomer);
                MqReceiver.Bind(Resources.CompanyUserQueue, Resources.CompanyUserRoute, ReactCompany);
                MqReceiver.Bind(Resources.CompanyComplaintQueue, Resources.CompanyComplaintRoute, ReactCompany);
                MqReceiver.Bind(Resources.CompanyBagQueue, Resources.CompanyBagRoute, ReactCompany);
        
            }

        }



    }
}